package com.huahua.bigdata.spark.rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Spark13_Operate_Transform_KV_sortByKey_2 {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        JavaSparkContext jsc = new JavaSparkContext(conf);

        List<Tuple2<String , Integer>> datas = new ArrayList<>();
        datas.add(new Tuple2<String , Integer>("a", 1));
        datas.add(new Tuple2<String , Integer>("a", 2));
        datas.add(new Tuple2<String , Integer>("a", 5));
        datas.add(new Tuple2<String , Integer>("a", 3));
        JavaRDD<Tuple2<String, Integer>> rdd = jsc.parallelize(datas);
        JavaPairRDD<String, Integer> pairRDD = rdd.mapToPair(t -> t);
        JavaPairRDD<Integer, Tuple2<String, Integer>> pairRDD1 = pairRDD.mapToPair(
                kv -> new Tuple2<>(kv._2, kv)
        );
        JavaPairRDD<Integer, Tuple2<String, Integer>> sortByKeyRDD = pairRDD1.sortByKey();

        sortByKeyRDD.map(t -> t._2).collect().forEach(System.out::println);

        jsc.close();
    }
}
